/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package it.ciano.util;

import java.io.*;
import java.util.*;

/**
 * A static class to manage file utilities.
 *
 * @author Luciano Xumerle
 * @version 0.0.1
 */
final public class CFile
{
    /**
     * The method parse a directory and adds entries to an ArrayList.
     *
     * @param fileList file are added to this
     * @param path the initial path to scan
     * @param rec find file recursively when true
     * @param onlyFile add to list only files. Skip directories.
     */
    public static void findFile ( ArrayList fileList, File path, boolean rec, boolean onlyFile )
    {
        if ( path.isDirectory() )
        {
            File aa[] = path.listFiles();
            Arrays.sort ( aa );
            for ( int i = 0; i < aa.length; i++ )
            {
                if ( rec )
                    findFile ( fileList, aa[ i ], rec, onlyFile );
                if ( ! ( onlyFile && aa[ i ].isDirectory() ) )
                    fileList.add ( aa[ i ] );
            }
        }
    }

    /**
     * The method checks if a string is a possible extension ".alphanumeric"
     *
     * @param str a possible extension
     * @return true or false
     */
    public static boolean isExtension ( String str )
    {
        int ln = str.length();
        char tt[] = str.toCharArray();
        // length>1 -> dot+1char
        // length<6 -> dot+max4 char (mpeg, info, etc)
        boolean pp = ( ln > 1 && ln < 6 && tt[ 0 ] == '.' );
        for ( int i = 1;i < ln; i++ )
            pp = ( pp && Character.isLetterOrDigit ( tt[ i ] ) );
        return pp;
    }

    /**
     * The method gets extension from a filename.
     *
     * @param filename string with filename
     * @return the filename's extension with dot
     */
    public static String getExtension ( String filename )
    {
        int pos = filename.lastIndexOf ( '.' );
        if ( pos < 0 )
            return "";
        String sub = filename.substring ( pos );
        if ( !isExtension ( sub ) )
            return "";
        // check for .tar
        if ( pos > 3 && filename.substring ( pos - 4, pos ).equals ( ".tar" ) )
            return ".tar" + sub;
        return sub;
    }

    /**
     * The method removes extension from a filename.
     *
     * @param filename string with filename
     * @return the filename without extension
     */
    public static String delExtension ( String filename )
    {
        return filename.substring ( 0, ( filename.length() - getExtension ( filename ).length() ) );
    }

    /**
     * Returns an ArrayList where every fiels is a fileline.
     *
     * @param filename filename
     * @return an ArrayList
     */
    public static ArrayList readTXT ( String filename )
    {
        ArrayList lines = new ArrayList();
        try
        {
            String line;
            FileReader fr = new FileReader ( filename );
            BufferedReader br = new BufferedReader ( fr );
            while ( null != ( line = br.readLine() ) )
                lines.add ( line.trim() );
        }
        catch ( IOException e )
        {
            return new ArrayList();
        }
        return lines;
    }

    /**
     * Prints the text contained into a String to the given file.
     *
     * @param filename name of destination file.
     * @param text String to be written into file.
     * @return true if all operations are OK.
     */
    public static boolean saveString ( String filename, String text )
    {
        PrintStream MyOutput = null;
        try
        {
            MyOutput = new PrintStream ( new FileOutputStream ( filename ) );
        }
        catch ( IOException e )
        {
            return false;
        }
        // List out sorted values in reverse order, with position numbers
        if ( MyOutput != null )
        {
            MyOutput.print ( text );
            MyOutput.close();
        }
        else
            return false;
        return true;
    }

} // END CLASS

